package com.tongda.menu.adapter;

import java.util.List;

import com.tongda.core.vo.Table;
import com.tongda.menu.R;
import com.tongda.menu.R.drawable;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class TableGridAdapter extends BaseAdapter {
	List<Table> tables;
	private Context context;
	
	
	public TableGridAdapter(Context context, List<Table> tables) {
		this.context = context;
		this.tables = tables;
	}

	public int getCount() {
		return tables != null ? tables.size() : 0;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	public Object getItem(int position) {
		return tables.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null || convertView.getTag() == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.table_grid_item, null);
			holder.no = (TextView) convertView.findViewById(R.id.table_no);
			holder.people_count = (TextView) convertView.findViewById(R.id.table_people_count);
			holder.table_time = (TextView) convertView.findViewById(R.id.table_time);
			holder.table_status = (TextView) convertView.findViewById(R.id.table_status);
			holder.table_click = (RelativeLayout) convertView.findViewById(R.id.table_status_icon);
			convertView.setTag(holder);
		} else {   
			holder = (ViewHolder) convertView.getTag();
		}
		Table table = tables.get(position);
		holder.no.setText(table.getTabNo());
		holder.people_count.setText(table.getPeopleCount() + "/" + table.getSeat());
		holder.table_time.setText(table.getOpenTableTime());
//		if(table.getOrderStatus().equals("未下单") || table.getOrderStatus().equals("已下单") || table.getOrderStatus().equals("已结账") || table.getOrderStatus().equals("反结账")){
//			holder.table_status.setText("使用中");
//			holder.table_click.setBackgroundResource(drawable.table_use);
//		}else{
//			holder.table_status.setText("空闲");
//			holder.table_click.setBackgroundResource(drawable.table_free);
//		}
		if(table.getDinnerStatus().equals("1")){
			holder.table_status.setText("空闲");
			holder.table_click.setBackgroundResource(drawable.table_free);
		}else if(table.getDinnerStatus().equals("5")){
			holder.table_status.setText("已结帐");
			holder.table_click.setBackgroundResource(drawable.table_settle);
		}else{
			holder.table_status.setText("使用中");
			holder.table_click.setBackgroundResource(drawable.table_use);
		}
		return convertView;
	}

	public final class ViewHolder {
		public RelativeLayout table_click;
		public TextView people_count;
		public TextView no;
		public TextView table_time;
		public TextView table_status;
	}

	public void setTables(List<Table> tables) {
		this.tables = tables;
	}

	public List<Table> getTables() {
		return tables;
	}

}
